package com.beiding.service;

import com.beiding.pojo.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.util.Date;

@Service
public class EmailServiceImpl implements EmailService {

    private JavaMailSenderImpl sender;

    @Autowired
    public void setSender(JavaMailSenderImpl sender) {
        this.sender = sender;
    }

    @Value("${spring.mail.username}")
    private String from;



    @Override
    public void send(Email email) {

        try {
            MimeMessage message = sender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(email.getReceiver());
            helper.setSubject(email.getSubject());
            helper.setSentDate(new Date(System.currentTimeMillis()));
            helper.setText(email.getMsg(), true);

            sender.send(message);

        } catch (Exception ignore) {

        }



    }

}
